package com.zegome.game.gomestory.main.scene.play.board;
import java.util.Random;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.zegome.game.action.IAction;
import com.zegome.game.action.IAction.IActionListener;
import com.zegome.game.action.interpolation.LinearFunction;
import com.zegome.game.action.interpolation.SineInFunction;
import com.zegome.game.action.interpolation.SineOutFunction;
import com.zegome.game.entity.IEntity;
import com.zegome.game.entity.action.LoopEntityAction;
import com.zegome.game.entity.action.MoveAction;
import com.zegome.game.entity.action.ScaleAction;
import com.zegome.game.entity.action.SequenceEntityAction;
import com.zegome.game.entity.sprite.BaseSprite;

public class Pikachu extends BaseSprite{
	
		Board board;
		public int type;
		int row, col;
		boolean isSelect;
		LoopEntityAction scaleAction;
		SequenceEntityAction diamondMove;
		MoveAction moveAction, moveAction2, moveAction3;				
		public boolean isMove;		
		
		boolean isSlash;
		int stateSlash;
		float tickSlash;
		float fpsSlash=0.08f;
		int frameSlash = 0;
		float fpsSleep;
		
		public Pikachu(Board board, float pX, float pY, int wi, int hi, int row, int col) {
			super(pX, pY);
			this.board = board;
			mWidth = wi;
			mHeight = hi;
			this.row = row;
			this.col = col;
			
			setOrigin(mWidth/2, mHeight/2);
		
			SequenceEntityAction smallerBigger = new SequenceEntityAction(
					new ScaleAction(0.4f, 0.8f, 1f, LinearFunction.getInstance()),
					new ScaleAction(0.4f, 1f, 0.8f, LinearFunction.getInstance()));
			scaleAction = new LoopEntityAction(smallerBigger);	
			moveAction = new MoveAction(2f, 0, 0, 0, 0, SineOutFunction.getInstance());	
			moveAction2 = new MoveAction(2f, 0, 0, 0, 0, SineInFunction.getInstance());	
			moveAction3 = new MoveAction(2f, 0, 0, 0, 0, SineInFunction.getInstance());	
			diamondMove = new SequenceEntityAction(moveAction2, moveAction3);
			
			moveAction3.addActionListener(new IActionListener<IEntity>() {
				
				@Override
				public void onActionStarted (IAction<IEntity> arg0, IEntity arg1) {
					// TODO Auto-generated method stub
				}
				
				@Override
				public void onActionFinished (IAction<IEntity> arg0, IEntity arg1) {
					// TODO Auto-generated method stub
					isMove = false;
				}
			});
			
			moveAction.addActionListener(new IActionListener<IEntity>() {
				
				@Override
				public void onActionStarted (IAction<IEntity> arg0, IEntity arg1) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onActionFinished (IAction<IEntity> arg0, IEntity arg1) {
					// TODO Auto-generated method stub
					isMove = false;
				}
			});
		}
		
		public void setSize(float wi, float hi){
			super.setSize(wi, hi);
			setOrigin(wi/2, hi/2);
		}
		
		public void setSlash(boolean isSlash){
			this.isSlash = isSlash;
		}
		
		public void set(int type)
		{
			this.type = type;
		}
		
		public void setSelect(boolean isSelect)
		{			
			if(this.isSelect == isSelect)
			{
				return;
			}
			this.isSelect = isSelect;
			if(isSelect == true){			
				this.registerAction(scaleAction);
			}
			else
			{				
				this.setScale(1f, 1f);
				this.unregisterAction(scaleAction);
			}
		}
		
		public void onUpdate(float pDeltaTime){
			if(isSlash){
				if(stateSlash == 0){
			if(tickSlash>=fpsSlash){
				tickSlash = 0;
				frameSlash++;
				if(frameSlash == 7){
					frameSlash = -1;
					stateSlash = 1;					
					fpsSleep = (new Random()).nextFloat()*2f;
				}
			}
			else{
				tickSlash+=pDeltaTime;
			}
				}
				else{
					if(tickSlash>=fpsSleep){
						tickSlash = 0;
						stateSlash = 0;
						frameSlash = 0;
					}
					else{
						tickSlash+=pDeltaTime;
					}
				}
			}
			super.onUpdate(pDeltaTime);
		}
//		boolean isLeft, isTop, isRight, isBottom;
		@Override		
		public void onDraw(final SpriteBatch pSpriteBatch, final float pAlpha) {
			if(type == -1)
				return;
			if (!isVisible)
				return;
			
//			isLeft = false;
//			isBottom = false;
//			isTop = false;
//			isRight = false;
//			
//			pSpriteBatch.setColor(1f, 1f, 1f, 1f);
//			
//			if(board.dataMap.map[row][col-1].type<0){
//				isLeft = true;
//				if(board.dataMap.map[row+1][col].type<0){
//					if(board.dataMap.map[row-1][col].type<0){						
//						pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*7);				
//					}
//					else
//						pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//				}
//				else if(board.dataMap.map[row-1][col].type<0){
//					pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//				}
//				else{
//					pSpriteBatch.draw(board.screen.ctr.a.eL, mX-board.dataMap.wiEdgeVer, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*9);				
//				}
//				
//			}
//			
//			if(board.dataMap.map[row][col+1].type<0){
//				isRight = true;
//				if(board.dataMap.map[row+1][col].type<0){
//					if(board.dataMap.map[row-1][col].type<0){						
//						pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*7);				
//					}
//					else
//						pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//				}
//				else if(board.dataMap.map[row-1][col].type<0){
//					pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY+board.dataMap.hiEdgeVerUnit, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*8);				
//				}
//				else{
//					pSpriteBatch.draw(board.screen.ctr.a.eR, mX+board.dataMap.wi1Pikachu, mY, board.dataMap.wiEdgeVer, board.dataMap.hiEdgeVerUnit*9);				
//				}
//				
//			}
//			
//			if(board.dataMap.map[row+1][col].type<0){
//				isTop = true;
//				if(board.dataMap.map[row][col+1].type<0){
//					if(board.dataMap.map[row][col-1].type<0){						
//						pSpriteBatch.draw(board.screen.ctr.a.eT, mX+board.dataMap.wiEdgeHorUnit, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*7, board.dataMap.wiEdgeVer);				
//					}
//					else
//						pSpriteBatch.draw(board.screen.ctr.a.eT, mX, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//				}
//				else if(board.dataMap.map[row][col-1].type<0){
//					pSpriteBatch.draw(board.screen.ctr.a.eT, mX+board.dataMap.wiEdgeHorUnit, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//				}
//				else{
//					pSpriteBatch.draw(board.screen.ctr.a.eT, mX, mY+board.dataMap.hi1Pikachu, board.dataMap.wiEdgeHorUnit*9, board.dataMap.wiEdgeVer);				
//				}
//				
//			}
//			
//			if(board.dataMap.map[row-1][col].type<0){
//				isBottom = true;
//				if(board.dataMap.map[row][col+1].type<0){
//					if(board.dataMap.map[row][col-1].type<0){						
//						pSpriteBatch.draw(board.screen.ctr.a.eB, mX+board.dataMap.wiEdgeHorUnit, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*7, board.dataMap.wiEdgeVer);				
//					}
//					else
//						pSpriteBatch.draw(board.screen.ctr.a.eB, mX, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//				}
//				else if(board.dataMap.map[row][col-1].type<0){
//					pSpriteBatch.draw(board.screen.ctr.a.eB, mX+board.dataMap.wiEdgeHorUnit, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*8, board.dataMap.wiEdgeVer);				
//				}
//				else{
//					pSpriteBatch.draw(board.screen.ctr.a.eB, mX, mY-board.dataMap.wiEdgeVer, board.dataMap.wiEdgeHorUnit*9, board.dataMap.wiEdgeVer);				
//				}
//				
//			}
//
//			if(isLeft && isTop){
//				pSpriteBatch.draw(board.screen.ctr.a.cTL, mX-board.dataMap.wiEdgeVer, mY+board.dataMap.hiEdgeVerUnit*8, board.dataMap.wiEdgeHorUnit*3f, board.dataMap.hiEdgeVerUnit*2.5f);				
//			}
//			
//			if(isRight && isTop){
//				pSpriteBatch.draw(board.screen.ctr.a.cTR, mX+board.dataMap.wi1Pikachu-board.dataMap.wiEdgeVer, mY+board.dataMap.hiEdgeVerUnit*8, board.dataMap.wiEdgeHorUnit*2.5f, board.dataMap.hiEdgeVerUnit*2.5f);				
//			}
//			
//			if(isLeft && isBottom){
//				pSpriteBatch.draw(board.screen.ctr.a.cBL, mX-board.dataMap.wiEdgeHorUnit*1.4f, mY-board.dataMap.hiEdgeVerUnit*1.55f, board.dataMap.wiEdgeHorUnit*2.5f, board.dataMap.hiEdgeVerUnit*2.5f);				
//			}
//			
//			if(isRight && isBottom){
//				pSpriteBatch.draw(board.screen.ctr.a.cBR, mX+board.dataMap.wiEdgeHorUnit*8f, mY-board.dataMap.hiEdgeVerUnit*1.5f, board.dataMap.wiEdgeHorUnit*2.5f, board.dataMap.hiEdgeVerUnit*2.5f);				
//			}
//			
//			pSpriteBatch.draw(board.screen.ctr.a.eL, mX, mY, board.dataMap.wi1Pikachu, board.dataMap.hi1Pikachu);
			
			
			pSpriteBatch.setColor(mColor.r, mColor.g, mColor.b, mColor.a * pAlpha);
			
						
			if (mScaleX == 1 && mScaleY == 1 && mRotation == 0){
				pSpriteBatch.draw(board.screen.ctr.a.squareRegion[isSelect?board.dataMap.squarePress:board.dataMap.squareNor], mX, mY, mWidth, mHeight);			
				pSpriteBatch.draw(board.img.pika[type], mX, mY, mWidth, mHeight);								
				if(isSlash && frameSlash>=0)
					pSpriteBatch.draw(board.screen.ctr.a.slash[frameSlash], mX, mY, mWidth, mHeight);
			}
			else {
				pSpriteBatch.draw(board.screen.ctr.a.squareRegion[isSelect?board.dataMap.squarePress:board.dataMap.squareNor], mX, mY, mOriginX, mOriginY, mWidth, mHeight,
					mScaleX, mScaleY, mRotation);
				pSpriteBatch.draw(board.img.pika[type], mX, mY, mOriginX, mOriginY, mWidth, mHeight,
						mScaleX, mScaleY, mRotation);
				if(isSlash && frameSlash>=0)
					pSpriteBatch.draw(board.screen.ctr.a.slash[frameSlash], mX, mY, mOriginX, mOriginY, mWidth, mHeight,
							mScaleX, mScaleY, mRotation);
			}
							
		}

		public void runFromTo(float duration, Vector2 from, Vector2 to)
		{
			isMove = true;
			setPosition(from.x, from.y);
			moveAction.reset(duration, from.x, to.x, from.y, to.y);						
			this.registerAction(moveAction);
		}
		
		public void runFromTo2(float slient, float duration, Vector2 from, Vector2 to)
		{
			isMove = true;
			setPosition(from.x, from.y);
			moveAction2.reset(slient, from.x, from.x, from.y, from.y);
			moveAction3.reset(duration, from.x, to.x, from.y, to.y);			
			diamondMove.reset();
			this.registerAction(diamondMove);
		}		
}
